
*=============================================================================

*** Setup

version 15
capture log close
set more off
clear all

* Load data
use "$data/fr_prep_import.dta"

*=============================================================================

*** Cost path

/* 
CPCP_T01_B1_S1_Distribution
T01 means conjoint task 1 (8 in total)
B1=own country, B2=other country
S1=scenario 1, S2=scenario 2
value of 9 means not asked
*/
label define cstpth_lab 1 "Constant" 2 "Increasing" 3 "Decreasing"

/* Loop over all 8 tasks*/
forvalues i=1(1)8 {

* Loop over scenarios 1 and 2
	forvalues j=1/2 {
	* Own country
gen own_cstpth_cj`i'_s`j'=CPCP_T0`i'_B1_S`j'_Distribution
replace own_cstpth_cj`i'_s`j'=. if CPCP_T0`i'_B1_S`j'_Distribution==9
label var own_cstpth_cj`i'_s`j' "Own country: cost path"
label values own_cstpth_cj`i'_s`j' cstpth_lab

* Other countries
gen other_cstpth_cj`i'_s`j'=CPCP_T0`i'_B2_S`j'_Distribution
replace other_cstpth_cj`i'_s`j'=. if CPCP_T0`i'_B2_S`j'_Distribution==9
label var other_cstpth_cj`i'_s`j' "Other country: cost path"
label values other_cstpth_cj`i'_s`j' cstpth_lab
	}
}

*=============================================================================

*** Household costs

* 9 means not asked
label define hhcsts_lab 1 "€28 per month" 2 "€56 per month" 3 "€113 per month" 4 "€141 per month"

/* Loop over all 8 tasks*/
forvalues i=1(1)8 {
* Scenarios 1 and 2
	forvalues j=1/2 {
	gen own_hhcsts_cj`i'_s`j'=CPCP_T0`i'_B1_S`j'_Cost
	replace own_hhcsts_cj`i'_s`j'=. if CPCP_T0`i'_B1_S`j'_Cost==9
	label var own_hhcsts_cj`i'_s`j' "Own country: costs per household"
	label values own_hhcsts_cj`i'_s`j' hhcsts_lab

	gen other_hhcsts_cj`i'_s`j'=CPCP_T0`i'_B2_S`j'_Cost
	replace other_hhcsts_cj`i'_s`j'=. if CPCP_T0`i'_B2_S`j'_Cost==9
	label var other_hhcsts_cj`i'_s`j' "Other country: cost path"
	label values other_hhcsts_cj`i'_s`j' hhcsts_lab
	}
}

*=============================================================================

*** Adaptation and mitigation mix

* Continuous from 0 to 100
* 999 means not asked

/* Loop over all 8 tasks*/
forvalues i=1(1)8 {
* Scenarios 1 and 2
	forvalues j=1/2 {
	gen own_adapt_cj`i'_s`j'=CPCP_T0`i'_B1_S`j'_Adapt
	replace own_adapt_cj`i'_s`j'=. if CPCP_T0`i'_B1_S`j'_Adapt==999
	label var own_adapt_cj`i'_s`j' "Own country: adaptation investment (%)"
	
	gen other_adapt_cj`i'_s`j'=CPCP_T0`i'_B2_S`j'_Adapt
	replace other_adapt_cj`i'_s`j'=. if CPCP_T0`i'_B2_S`j'_Adapt==999
	label var other_adapt_cj`i'_s`j' "Other country:  adaptation investment (%)"
	}
}

/* Loop over all 8 tasks*/
forvalues i=1(1)8 {
* Scenarios 1 and 2
	forvalues j=1/2 {
	gen own_mitig_cj`i'_s`j'=CPCP_T0`i'_B1_S`j'_Mitigate
	replace own_mitig_cj`i'_s`j'=. if CPCP_T0`i'_B1_S`j'_Mitigate==999
	label var own_mitig_cj`i'_s`j' "Own country: mitigation investment (%)"
	
	gen other_mitig_cj`i'_s`j'=CPCP_T0`i'_B2_S`j'_Mitigate
	replace other_mitig_cj`i'_s`j'=. if CPCP_T0`i'_B2_S`j'_Mitigate==999
	label var other_mitig_cj`i'_s`j' "Other country:  mitigation investment (%)"
	}
}

*=============================================================================

*** Outcome

* 1 means scenario 1, 2 means scenario 2, 9 is not asked

/* Loop over all 8 tasks*/
forvalues i=1/8 {
	*  Preference
	* If 1, then means that scenario one was chosen
	recode 	CPCP_T0`i' (1=1) (2=0) (9=.), gen(choice_cj`i'_s1)
	label var choice_cj`i'_s1 "Scenario 1 chosen"
	* If 2, then means that scenario two was chosen
	* recode 	CPCP_T0`i' (2=1) (1=0) (9=.), gen(choice_cj`i'_s2)
	recode choice_cj`i'_s1 (1=0) (0=1), gen(choice_cj`i'_s2)
	label var choice_cj`i'_s2 "Scenario 2 chosen"

	}


*=============================================================================

*** Reshape to conjoint level (wide to long)

* List all conjoint attribute level variables
local owncstpath="own_cstpth_cj1_s own_cstpth_cj2_s own_cstpth_cj3_s own_cstpth_cj4_s own_cstpth_cj5_s own_cstpth_cj6_s own_cstpth_cj7_s own_cstpth_cj8_s"
local othercstpath="other_cstpth_cj1_s other_cstpth_cj2_s other_cstpth_cj3_s other_cstpth_cj4_s other_cstpth_cj5_s other_cstpth_cj6_s other_cstpth_cj7_s other_cstpth_cj8_s" 
local owncsttmng="own_csttmng_cj1_s own_csttmng_cj2_s own_csttmng_cj3_s own_csttmng_cj4_s own_csttmng_cj5_s own_csttmng_cj6_s own_csttmng_cj7_s own_csttmng_cj8_s"
local othercsttmng="other_csttmng_cj1_s other_csttmng_cj2_s other_csttmng_cj3_s other_csttmng_cj4_s other_csttmng_cj5_s other_csttmng_cj6_s other_csttmng_cj7_s other_csttmng_cj8_s"
local ownhhcsts="own_hhcsts_cj1_s own_hhcsts_cj2_s own_hhcsts_cj3_s own_hhcsts_cj4_s own_hhcsts_cj5_s own_hhcsts_cj6_s own_hhcsts_cj7_s own_hhcsts_cj8_s"
local otherhhcsts="other_hhcsts_cj1_s other_hhcsts_cj2_s other_hhcsts_cj3_s other_hhcsts_cj4_s other_hhcsts_cj5_s other_hhcsts_cj6_s other_hhcsts_cj7_s other_hhcsts_cj8_s"
local ownadapt="own_adapt_cj1_s own_adapt_cj2_s own_adapt_cj3_s own_adapt_cj4_s own_adapt_cj5_s own_adapt_cj6_s own_adapt_cj7_s own_adapt_cj8_s"
local otheradapt="other_adapt_cj1_s other_adapt_cj2_s other_adapt_cj3_s other_adapt_cj4_s other_adapt_cj5_s other_adapt_cj6_s other_adapt_cj7_s other_adapt_cj8_s" 
local ownmitig="own_mitig_cj1_s own_mitig_cj2_s own_mitig_cj3_s own_mitig_cj4_s own_mitig_cj5_s own_mitig_cj6_s own_mitig_cj7_s own_mitig_cj8_s"
local othermitig="other_mitig_cj1_s other_mitig_cj2_s other_mitig_cj3_s other_mitig_cj4_s other_mitig_cj5_s other_mitig_cj6_s other_mitig_cj7_s other_mitig_cj8_s" 
local choice="choice_cj1_s choice_cj2_s choice_cj3_s choice_cj4_s choice_cj5_s choice_cj6_s choice_cj7_s choice_cj8_s"

reshape long `owncstpath' `othercstpath' `owncsttmng' `othercsttmng' `ownhhcsts' `otherhhcsts' `ownadapt' `otheradapt' `ownmitig' `othermitig' `choice', i(caseid) j(scenario)

*=============================================================================

*** Reshape to scenario level

* List all scenario attribute level variables
local owncstpath="own_cstpth_cj1 own_cstpth_cj2 own_cstpth_cj3 own_cstpth_cj4 own_cstpth_cj5 own_cstpth_cj6 own_cstpth_cj7 own_cstpth_cj8"
local othercstpath="other_cstpth_cj1 other_cstpth_cj2 other_cstpth_cj3 other_cstpth_cj4 other_cstpth_cj5 other_cstpth_cj6 other_cstpth_cj7 other_cstpth_cj8" 
local owncsttmng="own_csttmng_cj1 own_csttmng_cj2 own_csttmng_cj3 own_csttmng_cj4 own_csttmng_cj5 own_csttmng_cj6 own_csttmng_cj7 own_csttmng_cj8"
local othercsttmng="other_csttmng_cj1 other_csttmng_cj2 other_csttmng_cj3 other_csttmng_cj4 other_csttmng_cj5 other_csttmng_cj6 other_csttmng_cj7 other_csttmng_cj8"
local ownhhcsts="own_hhcsts_cj1 own_hhcsts_cj2 own_hhcsts_cj3 own_hhcsts_cj4 own_hhcsts_cj5 own_hhcsts_cj6 own_hhcsts_cj7 own_hhcsts_cj8"
local otherhhcsts="other_hhcsts_cj1 other_hhcsts_cj2 other_hhcsts_cj3 other_hhcsts_cj4 other_hhcsts_cj5 other_hhcsts_cj6 other_hhcsts_cj7 other_hhcsts_cj8"
local ownadapt="own_adapt_cj1 own_adapt_cj2 own_adapt_cj3 own_adapt_cj4 own_adapt_cj5 own_adapt_cj6 own_adapt_cj7 own_adapt_cj8"
local otheradapt="other_adapt_cj1 other_adapt_cj2 other_adapt_cj3 other_adapt_cj4 other_adapt_cj5 other_adapt_cj6 other_adapt_cj7 other_adapt_cj8" 
local ownmitig="own_mitig_cj1 own_mitig_cj2 own_mitig_cj3 own_mitig_cj4 own_mitig_cj5 own_mitig_cj6 own_mitig_cj7 own_mitig_cj8"
local othermitig="other_mitig_cj1 other_mitig_cj2 other_mitig_cj3 other_mitig_cj4 other_mitig_cj5 other_mitig_cj6 other_mitig_cj7 other_mitig_cj8" 
local choice="choice_cj1 choice_cj2 choice_cj3 choice_cj4 choice_cj5 choice_cj6 choice_cj7 choice_cj8"

foreach stub in `owncstpath' `othercstpath' `owncsttmng' `othercsttmng' `ownhhcsts' `otherhhcsts' `ownadapt' `otheradapt' `ownmitig' `othermitig' `choice' {
rename `stub'_s `stub'
}

reshape long own_cstpth_cj other_cstpth_cj own_csttmng_cj other_csttmng_cj own_hhcsts_cj other_hhcsts_cj own_adapt_cj other_adapt_cj own_mitig_cj other_mitig_cj choice_cj, i(caseid scenario) j(conjoint)

*=============================================================================

*** Generate common variables

foreach pre in own other {
tab `pre'_cstpth_cj, gen(`pre'_cstpth_)
rename `pre'_cstpth_1 `pre'_cstpth_const
rename `pre'_cstpth_2 `pre'_cstpth_inc
rename `pre'_cstpth_3 `pre'_cstpth_dec

tab `pre'_hhcsts_cj, gen(`pre'_hhcsts_)
rename `pre'_hhcsts_1 `pre'_hhcsts_low
rename `pre'_hhcsts_2 `pre'_hhcsts_med
rename `pre'_hhcsts_3 `pre'_hhcsts_high
rename `pre'_hhcsts_4 `pre'_hhcsts_vhigh
}

* Conjoint choice
gen choice_cj100=choice_cj*100

label values own_cstpth_cj cstpth_lab
label values other_cstpth_cj cstpth_lab

* Buckets for adaptation vs mitigation
lab define adapt_levels_lab 1 "0-19%" 2 "20-39%" 3 "40-59%" 4 "60-79%" 5 "80-100%"
foreach var in own_adapt_cj other_adapt_cj own_mitig_cj other_mitig_cj {
su `var', det
gen `var'_0_19=0
replace `var'_0_19=1 if `var'<20
gen `var'_20_39=0
replace `var'_20_39=1 if `var'>=20 & `var'<40
gen `var'_40_59=0
replace `var'_40_59=1 if `var'>40 & `var'<60
gen `var'_60_79=0
replace `var'_60_79=1 if `var'>60 & `var'<80
gen `var'_80_100=0
replace `var'_80_100=1 if `var'>80 & `var'<=100

gen `var'_5levels=.
replace `var'_5levels=1 if `var'<=20
replace `var'_5levels=2 if `var'>20 & `var'<=40
replace `var'_5levels=3 if `var'>40 & `var'<=60
replace `var'_5levels=4 if `var'>60 & `var'<=80
replace `var'_5levels=5 if `var'>80 & `var'<=100
lab values `var'_5levels adapt_levels_lab
}

*=============================================================================

*** Saving

save "$data/fr_scenariolevel.dta", replace
